package mspbots.cw.client.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import mspbots.cw.common.RabbitKeys;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * com.mspbots.sync.config.RabbitConfigurer
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/29
 */
@Configuration
public class RabbitConfiguration {

    private final AmqpAdmin amqpAdmin;
    private final Exchange exchange;

    Map<String, Object> args = new HashMap<>(2) {{
        put("x-dead-letter-exchange", RabbitKeys.BASE_EXCHANGE_NAME);
        put("x-dead-letter-routing-key", RabbitKeys.BAD_MESSAGE_QUEUE_KEY);
    }};

    public RabbitConfiguration(AmqpAdmin amqpAdmin) {
        this.amqpAdmin = amqpAdmin;
        this.exchange = ExchangeBuilder.directExchange(RabbitKeys.BASE_EXCHANGE_NAME).build();
        this.amqpAdmin.declareExchange(exchange);
    }

    @Bean
    public void declareBaseExchange() {
        Queue queue = QueueBuilder.durable("connect-wise.bad.message")
                .withArgument("x-message-ttl", 1000 * 60 * 60 * 24).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.BAD_MESSAGE_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.logs.task.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.LOGS_TASK_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.distributed.task.sync")
                .withArgument("x-message-ttl", 1000 * 60 * 60 * 3)
                .withArguments(args).build();
        this.amqpAdmin.deleteQueue(queue.getName());
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.DISTRIBUTED_TASK_QUEUE_KEY).noargs());
    }

    @Bean
    public void declareQueue1() {
        Queue queue = QueueBuilder.durable("connect-wise.boards.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.BOARDS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.boards.associations.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.ASSOCIATIONS_BOARDS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.users.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.USERS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.tickets.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.TICKETS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.companies.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.COMPANIES_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.agreements.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.AGREEMENTS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.contacts.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.CONTACTS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.time-entries.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.TIME_ENTRIES_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.sub-data.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange)
                .with(RabbitKeys.SUB_DATA_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.projects-phases.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.PROJECTS_PHASES_SYNC_QUEUE_KEY).noargs());
    }

    @Bean
    public void declareQueue2() {

        Queue queue = QueueBuilder.durable("connect-wise.schedules.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.SCHEDULES_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.orders.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.ORDERS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.invoices.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.INVOICES_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.activities.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.ACTIVITIES_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.time-sheets.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.TIME_SHEETS_SYNC_QUEUE_KEY).noargs());

    }

    @Bean
    public void declareQueue3() {

        Queue queue = QueueBuilder.durable("connect-wise.purchase-orders.sync")
                .withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.PURCHASE_ORDERS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.board-auto-assign-resource.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.AUTO_ASSIGN_RESOURCE_BOARDS_SYNC_QUEUE_KEY).noargs());

        queue = QueueBuilder.durable("connect-wise.reports.sync").withArguments(args).build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with(RabbitKeys.REPORTS_SYNC_QUEUE_KEY).noargs());

    }

    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}
